package com.it.jzs.service.admin.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.jzs.dao.admin.AdminLinkMapper;
import com.it.jzs.dao.admin.AdminSwiperMapper;
import com.it.jzs.pojo.Link;
import com.it.jzs.pojo.Swiper;
import com.it.jzs.pojo.Type;
import com.it.jzs.service.admin.AdminLinkService;
import com.it.jzs.service.admin.AdminSwiperService;
import com.it.jzs.vo.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

@Service
public class AdminSwiperServiceImpl implements AdminSwiperService {
    @Resource
    private AdminSwiperMapper adminSwiperMapper;

    @Override
    public Result<Object> selectPage(Integer index, Integer size) {

        QueryWrapper<Swiper> wrapper = new QueryWrapper<>();
        Page<Swiper> page = new Page<>(index, size);

        Page<Swiper> typePage = adminSwiperMapper.selectPage(page, wrapper);
        return Result.success(typePage, "获取分类分页数据成功");
    }



    @Override
    public Result<Object> insertOne(Swiper swiper) {
        String swiperName = swiper.getSwiperName();
//        link.get
        if (StringUtils.isEmpty(swiperName)) {
            return Result.error(null, "数据不合法");
        }
        swiper.setCreateTime(System.currentTimeMillis());
        swiper.setUpdateTime(System.currentTimeMillis());
        int insert = adminSwiperMapper.insert(swiper);
        if (insert > 0) {
            return Result.success(null, "新增友链成功");
        }
        return Result.error(null, "新增友链失败");
    }

    @Override
    @Transactional
    public Result<Object> delete(Integer id) {
        if (id != null) {
//                Type type = typeMapper.selectById(id);
            int i = adminSwiperMapper.deleteById(id);
            if (i > 0) {
                return Result.success(null, "删除成功！");
            }
        }
        return Result.error(null, "删除失败！");
    }



    @Override
    @Transactional
    public Result<Object> update(Swiper swiper) {  // 修改分类

        String swiperName = swiper.getSwiperName();
//        link.get
        if (StringUtils.isEmpty(swiperName)) {
            return Result.error(null, "数据不合法");
        }
        QueryWrapper<Type> wrapper = new QueryWrapper<>();
        swiper.setUpdateTime(System.currentTimeMillis());
        int update = adminSwiperMapper.updateById(swiper);
        if (update > 0) {
            return Result.success(null, "修改成功");
        }
        return Result.error(null, "修改失败");
//        int i = typeMapper.updateById(type);


    }
}
